#ifndef TICKETEXPLORER_H
#define TICKETEXPLORER_H

#include <QtCore/QObject>
#include <QtCore/QStringList>

#include <QtGui/QLineEdit>
#include <QtGui/QComboBox>
#include <QtGui/QRadioButton>

#include "ticketinterface.h"

struct TicketField {
    QString label;
    QString name;
    QStringList options;
    QString type;
    QString defaultValue;

    bool hasOptions() const { return !options.isEmpty(); }
};

inline bool operator<(const TicketField &field, const TicketField &field2)
{
    return field.name < field2.name;
}

inline bool operator<(const TicketField &field, const QString &name)
{
    return field.name < name;
}

inline bool operator<(const QString &name, const TicketField &field)
{
    return name < field.name;
}

class Ticket;
class TicketChangeset;
class TicketQuery;
class QAbstractItemModel;
class QUrl;

class TicketExplorerPrivate;
class TicketExplorer : public QObject
{
    Q_OBJECT
public:
    // TicketExplorer takes ownership of backend.
    explicit TicketExplorer(ITicketExplorerBackend *backend);
    ~TicketExplorer();

    // Returns the used backend's ID.
    QString backendId() const;

    // Executes query. The item model is populated asynchronously.
    BaseTicketItemModel *modelQuery(TicketQuery query) const;

    // Returns the default item delegate for the active backend
    QAbstractItemDelegate *ticketListDelegate() const;

    // Returns a newly created ticket. This is merely a shell for a ticket,
    // you must populate it and save it yourself.
    Ticket *createTicket() const;

    // Requests a list of tickets changed since 'since' - emits changedTickets
    // signal when done.
    void listChangedTickets(const QDateTime &since) const;

    // Might include actions. Emits fieldDefinitionsUpdated when done.
    // These are loaded automatically when the TicketExplorer is constructed,
    // use updateFieldDefinitions for a refresh.
    void updateFieldDefinitions();

    // Read the list of available ticket fields.
    QList<TicketField> getAvailableFields() const;

    // Return configuration items. Some backends may not use remote URLs or usernames
    // and will simply return empty values.
    QUrl remoteUrl() const;
    QString username() const;

signals:
    // Emitted when field definitions are first loaded or after
    // a updateFieldDefinitions call.
    void fieldDefinitionsUpdated();

    // Emitted after a listChangedTickets call.
    void changedTickets(const QList<int> &tickets);

    // Error
    void error(const TicketError &error);

private slots:
    void updateTicket(Ticket *ticket);

private:
    TicketExplorerPrivate *d;
    friend class TicketExplorerPrivate;
};



#endif // TICKETEXPLORER_H
